package com.jetpackframework.ioc;

import android.view.View;
import android.widget.AdapterView;
import android.widget.CompoundButton;

public class EventListener implements View.OnClickListener,View.OnLongClickListener, AdapterView.OnItemClickListener,AdapterView.OnItemSelectedListener, CompoundButton.OnCheckedChangeListener{
    private View.OnClickListener onClickListener;
    private View.OnLongClickListener onLongClickListener;
    private AdapterView.OnItemClickListener onItemClickListener;
    private AdapterView.OnItemSelectedListener onItemSelectedListener;
    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener;

    private int clickDelayTime;
    private long lastClickTime;

    public EventListener(View.OnClickListener onClickListener,int clickDelayTime){
        this.onClickListener = onClickListener;
        this.clickDelayTime = clickDelayTime;
    }
    public EventListener(View.OnLongClickListener onLongClickListener,int clickDelayTime){
        this.onLongClickListener = onLongClickListener;
        this.clickDelayTime = clickDelayTime;
    }
    public EventListener(AdapterView.OnItemClickListener onItemClickListener,int clickDelayTime){
        this.onItemClickListener = onItemClickListener;
        this.clickDelayTime = clickDelayTime;
    }
    public EventListener(AdapterView.OnItemSelectedListener onItemSelectedListener,int clickDelayTime){
        this.onItemSelectedListener = onItemSelectedListener;
        this.clickDelayTime = clickDelayTime;
    }
    public EventListener(CompoundButton.OnCheckedChangeListener onCheckedChangeListener,int clickDelayTime){
        this.onCheckedChangeListener = onCheckedChangeListener;
        this.clickDelayTime = clickDelayTime;
    }

    @Override
    public void onClick(View v) {
        if (clickDelayTime != 0){
            if (System.currentTimeMillis() - lastClickTime > clickDelayTime){
                if (onClickListener != null){
                    lastClickTime = System.currentTimeMillis();
                    onClickListener.onClick(v);
                    return;
                }
            }
        }else {
            if (onClickListener != null){
                onClickListener.onClick(v);
            }
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (clickDelayTime != 0){
            if (System.currentTimeMillis() - lastClickTime > clickDelayTime){
                if (onLongClickListener != null){
                    lastClickTime = System.currentTimeMillis();
                    onLongClickListener.onLongClick(v);
                    return false;
                }
            }
        }else {
            if (onLongClickListener != null){
                onLongClickListener.onLongClick(v);
            }
        }
        return false;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (clickDelayTime != 0){
            if (System.currentTimeMillis() - lastClickTime > clickDelayTime){
                if (onItemClickListener != null){
                    lastClickTime = System.currentTimeMillis();
                    onItemClickListener.onItemClick(parent,view,position,id);
                    return;
                }
            }
        }else {
            if (onItemClickListener != null){
                onItemClickListener.onItemClick(parent,view,position,id);
            }
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if (clickDelayTime != 0){
            if (System.currentTimeMillis() - lastClickTime > clickDelayTime){
                if (onItemSelectedListener != null){
                    lastClickTime = System.currentTimeMillis();
                    onItemSelectedListener.onItemSelected(parent,view,position,id);
                    return;
                }
            }
        }else {
            if (onItemSelectedListener != null){
                onItemSelectedListener.onItemSelected(parent,view,position,id);
            }
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        if (onItemSelectedListener != null) {
            onItemSelectedListener.onNothingSelected(parent);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (clickDelayTime != 0){
            if (System.currentTimeMillis() - lastClickTime > clickDelayTime){
                if (onCheckedChangeListener != null){
                    lastClickTime = System.currentTimeMillis();
                    onCheckedChangeListener.onCheckedChanged(buttonView,isChecked);
                    return;
                }
            }
        }else {
            if (onCheckedChangeListener != null){
                onCheckedChangeListener.onCheckedChanged(buttonView,isChecked);
            }
        }
    }
}
